﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.SalesManagement;
using MediatR;
using Sell.Api.Read.Application.Command.ContractCommand;
using Sell.DTO;
using SqlSugar;
using System.Diagnostics.Contracts;
using Contract = ERP.Domain.SalesManagement.Contract;

namespace Sell.Api.Read.Application.CommandHandlers.ContractCommandHandlers
{
    public class ContractlistCommandHandlers : IRequestHandler<ContractlistCommand, ApiResult<ApiPaging<contractlistdto>>>
    {
        public ISqlSugarClient SqlSugar { get; }
        public ContractlistCommandHandlers(ISqlSugarClient sqlSugar)
        {
            SqlSugar = sqlSugar;
        }
        /// <summary>
        /// 采购合同列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<ApiPaging<contractlistdto>>> Handle(ContractlistCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<contractlistdto>> res = new ApiResult<ApiPaging<contractlistdto>>();
            res.Code=EnumCode.Ok;
            res.Message = "采购合同列表成功";
            /// 初始化查询：关联Contract表和Supplier表
            var query = SqlSugar.Queryable<Contract>()
                .LeftJoin<Supplier>((c, s) => c.SupplierId == s.Id)
                .Where(c => c.IsDelete == false).Select(
                (c, s) => new contractlistdto
                {
                     Accessory=c.Accessory,
                      ContractCode=c.ContractCode,
                       ContractName=c.ContractName,
                        ContractPrice=c.ContractPrice,
                         CreateTime=c.CreateTime,
                          Desc=c.Desc,
                           Id=c.Id,
                            IsDelete=c.IsDelete,
                             MaterialCount=c.MaterialCount,
                              MaterialId=c.MaterialId,
                               People=s.People,
                                Phone=s.Phone,
                                 Review=c.Review,
                                  State=s.State,
                                   SupplierCode=s.SupplierCode,
                                    SupplierId=c.SupplierId,
                                     SupplierLevel=s.SupplierLevel,
                                      SupplierName=s.SupplierName,
                                       UpdateTime=c.UpdateTime,
                });
            if (!string.IsNullOrEmpty(request.ContractName))
            {
                query = query.Where(c => c.ContractName.Contains(request.ContractName));
            }
            if (request.SupplierId != null)
            {
                query = query.Where(c => c.SupplierId.Equals(request.SupplierId));
            }
            //SqlSugar分页查询实现
            ApiPaging<contractlistdto> result = new ApiPaging<contractlistdto>();
            result.TotalCount = query.Count();//查询总记录数
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.pagesize);//计算总页数
            result.Data = query.Skip((request.pageindex - 1) * request.pagesize)//跳过前面的记录
                .Take(request.pagesize)//取指定页大小的记录
                .ToList();//执行查询并转为List
            res.Success(result, "查询成功");
            return res;
        }
    }
}
